package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.yxinsur.imc.business.domain.ImcBrokerRole;
import com.yxinsur.imc.business.domain.ImcRole;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.ImcRoleForm;
import com.yxinsur.imc.business.mapper.ImcBrokerRoleMapper;
import com.yxinsur.imc.business.mapper.ImcRoleMapper;
import com.yxinsur.imc.business.service.IImcRoleService;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 代理人身份角色码Service业务层处理
 *
 * @author imc
 * @date 2021-04-13
 */
@Service
public class ImcRoleServiceImpl extends ServiceImpl<ImcRoleMapper, ImcRole> implements IImcRoleService
{
    @Autowired
    private ImcRoleMapper imcRoleMapper;
    @Autowired
    private ImcBrokerRoleMapper imcBrokerRoleMapper;

    /**
     * 查询代理人身份角色码
     *
     * @param id 代理人身份角色码ID
     * @return 代理人身份角色码
     */
    @Override
    public ImcRole selectImcRoleById(Long id)
    {
        return imcRoleMapper.selectImcRoleById(id);
    }

    /**
     * 查询代理人身份角色码列表
     *
     * @param imcRole 代理人身份角色码
     * @return 代理人身份角色码
     */
    @Override
    public List<ImcRole> selectImcRoleList(ImcRole imcRole)
    {
        imcRole.setTenantId(SecurityUtils.getTenantId());
        imcRole.setIsDeleted(IsDeletedEnum.N.getCode());
        return imcRoleMapper.selectImcRoleList(imcRole);
    }

    /**
     * 新增代理人身份角色码
     *
     * @param imcRoleForm 代理人身份角色码
     * @return 结果
     */
    @Override
    public int insertImcRole(ImcRoleForm imcRoleForm) {
        // isdeleted 相同租户下不能重名
        ImcRole imcRole = new ImcRole();
        imcRole.setTenantId(SecurityUtils.getTenantId());
        imcRole.setBrokerRoleName(imcRoleForm.getBrokerRoleName());
        imcRole.setCreateId(SecurityUtils.getUsername());
        imcRole.setUpdateId(SecurityUtils.getUsername());
        return imcRoleMapper.insertImcRole(imcRole);


    }

    /**
     * 修改代理人身份角色码
     *
     * @param imcRoleForm 代理人身份角色码
     * @return 结果
     */
    @Override
    public int updateImcRole(ImcRoleForm imcRoleForm) {
        ImcRole imcRole = new ImcRole();
        imcRole.setId(imcRoleForm.getId());
        imcRole.setBrokerRoleName(imcRoleForm.getBrokerRoleName());
        return imcRoleMapper.updateImcRole(imcRole);
    }

    /**
     * 批量删除代理人身份角色码
     *
     * @param ids 需要删除的代理人身份角色码ID
     * @return 结果
     */
    @Override
    public int deleteImcRoleByIds(Long[] ids) {
        List<ImcBrokerRole> brokerRoles = imcBrokerRoleMapper.selectImcBrokerRoleListByRoleIds(ids);
        if(!CollectionUtils.isEmpty(brokerRoles)){
            return 0;
        }
        //“id”+”废弃“+name  isdeleted =id
        int count=0;
        for (Long id : ids) {
            ImcRole imcRole = new ImcRole();
            ImcRole oldRole = imcRoleMapper.selectImcRoleById(id);
            imcRole.setId(id);
            imcRole.setBrokerRoleName(id+"-废弃-"+oldRole.getBrokerRoleName());
            imcRole.setIsDeleted(String.valueOf(id));
            imcRole.setUpdateTime(DateUtils.getNowDate());
            count+=imcRoleMapper.updateImcRole(imcRole);
        }
        return count;

    }

    /**
     * 删除代理人身份角色码信息
     *
     * @param id 代理人身份角色码ID
     * @return 结果
     */
    @Override
    public int deleteImcRoleById(String id)
    {
        return imcRoleMapper.deleteImcRoleById(id);
    }

    @Override
    public int deleteLogicRole(String id) {
        return imcRoleMapper.deleteLogicRole(id);
    }


}
